package com.hanhai.material.device;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBom;
import com.hanhai.material.device.entity.SysWorkPlan;
import com.hanhai.material.device.utils.kingdee.KingdeeUtils;
import com.hanhai.material.utils.redis.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/kingdee")
@Log4j2
public class KingdeeController {

    @Autowired
    KingdeeUtils kingdeeUtils;

    @Value("${kingdee.baseUrl}")
    String baseUrl;
    @Value("${kingdee.authorityCode}")
    String authorityCode;

    @Value("${kingdee.fmanagerid}")
    private String fManagerId;
    @Value("${kingdee.smanagerid}")
    private String sManagerId;
    @Autowired
    RedisUtil redisUtil;


    @GetMapping("/material/{materialId}")
    public Result<?> getMaterialDetail(@PathVariable String materialId) throws IOException {
        JSONObject jsonObject=this.kingdeeUtils.getMaterialDetail(materialId);
        return jsonObject==null?ResultTool.fail():ResultTool.success(jsonObject);
    }

    @PostMapping("/material/list/{size}")
    public Result<?> getMaterialList(@PathVariable Integer size, @RequestBody SysWorkPlan workPlan) throws IOException {
//        return this.getPPBOMList(size,workPlan);
        return ResultTool.success();
    }
    public Result<?> getPPBOMList(int size,SysWorkPlan workPlan) throws IOException {
        String token = this.kingdeeUtils.createToken();

        String url = baseUrl + "/PPBOM/GetList?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object, Object> map = MapBuilder.create().put("Data",
                MapBuilder.create()
                        .put("Top", size)
                        .put("PageSize", size)
                        .put("PageIndex", "1")
                        .put("OrderBy", "[FBillNo] DESC")
                        .put("SelectPage", "1").build()).build();
        HttpEntity<Map<Object, Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:" + jsonObject.getJSONObject("body"));
        JSONObject body = jsonObject.getJSONObject("body");
        log.error("Json:" + body);
        JSONObject Data = body.getJSONObject("Data");
        log.error("Data:" + Data);
        JSONArray jsonArray = Data.getJSONArray("DATA");
        log.error("JSONArray:" + jsonArray);
        SysBom sysBom=null;
        for (Object object : jsonArray) {

            String billNo = JSONObject.parseObject(JSONObject.toJSONString(object)).getString("FBillNo");

            sysBom=this.kingdeeUtils.getPPBOMDetail1(billNo);


           if(sysBom.getProductIcmoId().equals(workPlan.getInterId())){
               return ResultTool.success();
           }



        }
        return sysBom==null?ResultTool.fail():ResultTool.success();

    }
}
